package com.taomeishu.mailcrazy.checkers.qq;

import org.json.JSONException;
import org.json.JSONObject;

import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.Mail;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public class QQResultParser implements ResultParser {

	@Override
	public CheckResult parse(ClientResponse clientResponse, String username, String domainName) throws ResultParseException {
		String s = new String(clientResponse.getData());
		CheckResult result = new CheckResult(true);
		Mail mail = new Mail(username, domainName);
		result.setMail(mail);
		try {
			JSONObject json = new JSONObject(s);
			int ec = json.getInt("ec");
			if (ec == 8) {
				// exist
				result.addExistMail(mail);
			} else {
				result.addNotExistMail(mail);
			}
		} catch (JSONException e) {
			throw new ResultParseException(e);
		}
		return result;
	}

}
